package com.novellotus.iov.carvice.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.AboutActivity;
import com.novellotus.iov.carvice.activity.FailureListActivity;
import com.novellotus.iov.carvice.activity.FeedBackActivity;
import com.novellotus.iov.carvice.activity.LoginActivity;
import com.novellotus.iov.carvice.activity.ModifyMobileActivity;
import com.novellotus.iov.carvice.activity.ModifyPasswordActivity;
import com.novellotus.iov.carvice.activity.MyIntegralActivity;
import com.novellotus.iov.carvice.activity.PayRecordListActivity;
import com.novellotus.iov.carvice.activity.PersonalActivity;
import com.novellotus.iov.carvice.bean.AccountBean;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_company)
    TextView tv_company;
    @BindView(R.id.tv_role)
    TextView tv_role;
    @BindView(R.id.tv_my_integral)
    TextView tv_my_integral;
    String url, des;
    String point = "0";

    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    View initRootView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    void init() {
        getTra();
       // getAccount();
    }

    @Override
    public void onResume() {
        super.onResume();
        tv_name.setText(SharedPreferencesUtils.getUser(getContext())[0]);
    }


    private void getAccount() {
        RetrofitFactory.getInstance().account(SharedPreferencesUtils.getKey(getContext())[0])
                .compose(RxSchedulers.<BaseEntity<AccountBean>>compose())
                .subscribe(new BaseObserver<AccountBean>(getContext()) {
                    @Override
                    public void onSuccess(AccountBean response) {
                        if (response != null) {
                            point = response.getPoint() + "";
                            tv_my_integral.setText("我的积分:" + response.getPoint());
                        }

                    }

                    @Override
                    public void onFail(int code, String msg) {
                        MyUtils.showToast(getContext(), msg);
                    }

                    @Override
                    public void onError(String msg) {
                        MyUtils.showToast(getContext(), msg);
                    }

                });
    }
    private void getTra(){
        RetrofitFactory.getInstance().trajectory()
                .compose(RxSchedulers.<BaseEntity<AccountBean>>compose())
                .subscribe(new BaseObserver<AccountBean>(getContext()) {
                    @Override
                    public void onSuccess(AccountBean response) {
                        if (response != null) {
                        }

                    }

                    @Override
                    public void onFail(int code, String msg) {
                        MyUtils.showToast(getContext(), msg);
                    }

                    @Override
                    public void onError(String msg) {
                        MyUtils.showToast(getContext(), msg);
                    }

                });
    }

    @OnClick(R.id.rlt_integral)
    void openIntegral() {
        Intent intent = new Intent(getActivity(), MyIntegralActivity.class);
        intent.putExtra("point", point);
        getActivity().startActivity(intent);
    }

    @OnClick(R.id.rlt_collection)
    void openCollection() {
        openActivity(getActivity(), MyIntegralActivity.class);
    }


    @OnClick(R.id.rlt_personal)
    void openPersonal() {
        openActivity(getActivity(), PersonalActivity.class);
    }

    @OnClick(R.id.rlt_password)
    void openPassword() {
        openActivity(getActivity(), ModifyPasswordActivity.class);
    }

    @OnClick(R.id.rlt_pay_record)
    void openPayRecord() {
//        MyUtils.showToast(getContext(), "开发中，敬请期待");
        openActivity(getActivity(), PayRecordListActivity.class);
    }

    @OnClick(R.id.rlt_phone)
    void openPhone() {
        openActivity(getActivity(), ModifyMobileActivity.class);
    }

    @OnClick(R.id.rlt_record)
    void openRecord() {
        openActivity(getActivity(), FailureListActivity.class);
    }

    @OnClick(R.id.rlt_feedback)
    void openFeedBack() {
        openActivity(getActivity(), FeedBackActivity.class);
    }

    AlertDialog alertDialog;
    void openServicePhone() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        alertDialog = builder.create();
        builder.setMessage("新荷平台客服电话:" + getResources().getString(R.string.phone));
        builder.setPositiveButton("拨打", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + getResources().getString(R.string.phone)));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                alertDialog.dismiss();
            }
        });

        builder.show();
    }

    @OnClick(R.id.rlt_about)
    void openAbout() {

        if (url != null && url.length() > 0) {
            Bundle bundle = new Bundle();
            bundle.putString("des", des);
            bundle.putString("url", url);
            openActivity(getActivity(), AboutActivity.class, bundle);
        } else {
            openActivity(getActivity(), AboutActivity.class);
        }
    }

    @OnClick(R.id.rlt_out_login)
    void outLogin() {
        openActivity(getActivity(), LoginActivity.class);
        SharedPreferencesUtils.deleteSharedPreferences(getContext());
        getActivity().finish();
    }

    public void setUpdate(String url, String des) {
        this.url = url;
        this.des = des;
    }

    public void setEnterprise(RxEinfos rxEinfos) {
        if (rxEinfos != null) {
            tv_company.setText(rxEinfos.getEname() + "");
            if (rxEinfos.getRoles() != null && rxEinfos.getRoles().size() > 0) {
                StringBuffer roles = new StringBuffer();
                for (RxEinfos.RolesBean rolesBean : rxEinfos.getRoles()) {
                    roles.append("#" + rolesBean.getRoleName());
                }
                tv_role.setText(roles);
            }
        } else {
            tv_company.setText("未加入企业");
            tv_role.setText("普通用户");
        }

    }
}
